<?php

require_once dirname(__FILE__) . '/loadphplib.php';

$arrUsers = array(
    'think667788@sina.com',
        #'think889977@yeah.net',
);

$uptime = shell_exec('uptime');
$need = true;
$needlog = false;
$arr = explode('load average:', $uptime);
$cputime = $arr[1];
$arrcputime = explode(',', $cputime);
foreach ($arrcputime as &$strcputime) {
    $strcputime = trim($strcputime);
}trim($strcputime);

foreach ($arrcputime as $strcputime) {
    if ($strcputime < 10) {
        $need = false;
    }
    if ($strcputime > 5) {
        $needlog = true;
    }
}

if ($needlog) {
    $t = date('Y-m-d H:i:s') . 'NOTICE:uptime[' . implode('|', $arrcputime) . ']' . PHP_EOL;
    file_put_contents(dirname(__FILE__) . '/loguptime.log', $t, 8);
}

$log = '<pre>'.date('Y-m-d H:i:s') . 'NOTICE:uptime[' . implode('|', $arrcputime) . ']<hr />' . PHP_EOL;

function get_httpd_count(){
	return intval(shell_exec('ps -ef | grep httpd | wc -l'));
}

if ($need) {
    $log.='LocalIp[<b>' . $localIp . '</b>]<hr />';
	$t = date('Y-m-d H:i:s') . 'NOTICE:Restart Httpd' . PHP_EOL;
	file_put_contents(dirname(__FILE__) . '/loguptime.log', $t, 8);

    $opts = array(
        'http' => array(
            'method' => "GET",
            'timeout' => 10,
        )
    );
    $context = stream_context_create($opts);

    error_reporting(E_ALL);
    $localIp = file_get_contents('http://imail.sinaapp.com/ip.php', false, $context);
    if (!$localIp) {
        $localIp = file_get_contents('http://imail.sinaapp.com/ip.php', false, $context);
    }

    $arrReportResult['mysqld_count'] = intval(shell_exec('ps -ef | grep mysql | wc -l'));
    $arrReportResult['httpd_count_before'] = intval(shell_exec('ps -ef | grep httpd | wc -l'));
    $arrReportResult['uptime'] = shell_exec('uptime');
    $arrReportResult['logsize'] = shell_exec('du -h /data/logs/ | tail -1');

    $log.=date('Y-m-d H:i:s') . 'NOTICE:Start Restart Apache...HttpdCount[' . $arrReportResult['httpd_count_before'] . ']<hr />' . PHP_EOL;

    echo shell_exec('cd /usr/sbin/ && apachectl stop');
    $log.=date('Y-m-d H:i:s') . 'NOTICE:Apache Stop Command Exec Success.<hr />' . PHP_EOL;
    $arrReportResult['httpd_count_after'] = intval(shell_exec('ps -ef | grep httpd | wc -l'));
    $log.=date('Y-m-d H:i:s') . 'NOTICE:After Stop Apache...HttpdCount[' . $arrReportResult['httpd_count_after'] . ']<hr />' . PHP_EOL;


	for($i=0;$i<=100;$i++){
		$hc=get_httpd_count();
		if($hc>1){
			sleep(1);
			$t = date('Y-m-d H:i:s') . 'NOTICE:Restart Sleep['.$i.'],Count['.$hc.']...' . PHP_EOL;
			$log.=date('Y-m-d H:i:s') . 'NOTICE:Restart Sleep['.$i.'],Count['.$hc.']...<hr />' . PHP_EOL;
			file_put_contents(dirname(__FILE__) . '/loguptime.log', $t, 8);
		}else{
			break;
		}
	}

	$hc=get_httpd_count();
	if($hc>1){
		$log.=date('Y-m-d H:i:s') . 'WARNING:Restart Failed,Count['.$hc.']...<hr />' . PHP_EOL;
	}

    echo shell_exec('cd /usr/sbin/ && apachectl start');
    $log.=date('Y-m-d H:i:s') . 'NOTICE:Apache Start Command Exec Success.<hr />' . PHP_EOL;
    $arrReportResult['httpd_count_restart'] = intval(shell_exec('ps -ef | grep httpd | wc -l'));
    $log.=date('Y-m-d H:i:s') . 'NOTICE:After Start Apache...HttpdCount[' . $arrReportResult['httpd_count_restart'] . ']<hr />' . PHP_EOL;

    $arrReportResult['mysqlconn'] = 'OK';
    $dbconn = mysql_connect('127.0.0.1', 'root', 'china@888') or $arrReportResult['mysqlconn'] = 'ERROR';

    $log.=print_r($arrReportResult, true);

    post_email_by_sae('服务器高负载自动重启Httpd[' . date('Y-m-d H:i:s') . ']', $log, $arrUsers);
    post_email_by_smtp('服务器高负载自动重启Httpd[' . date('Y-m-d H:i:s') . ']', $log, $arrUsers);
}


